<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

$command = isset($options[1]) ? $options[1] : null;

switch ($command) {
  case 'save_perms':
    $rs = $db->GetCol("SELECT rid FROM core_roles");
    foreach ($rs as $rid) {
      $data['rid'] = $rid;
      $data['perms'] = isset($_POST['perms'][$rid]) ? serialize($_POST['perms'][$rid]) : null;
      if (isset($_POST['rid'][$rid])) {
        $rs = @$db->AutoExecute('core_permissions', $data, 'INSERT');
        if(!$rs) {
          $rs = $db->AutoExecute('core_permissions', $data, 'UPDATE', "rid='$rid'");
        }
      }
    }
    $tpl->assign('msg', $db->ErrorMsg());
    $this->tpl->assign('result',true);
    $db->CacheFlush();
  break;
  default:
    /**
     * pager (show 3 roles per page)
     */
    $num_of_roles = $db->GetOne("SELECT count(*) FROM core_roles");
    $pageno = isset($options[1]) ? $options[1] : 1;
    $roles_per_page = 3;
    $lastpage = ceil($num_of_roles/$roles_per_page);
    $pageno = (int)$pageno;
    if ($pageno < 1) {
      $pageno = 1;
    } elseif ($pageno > $lastpage) {
      $pageno = $lastpage;
    }
    $limit = 'LIMIT '.($pageno - 1) * $roles_per_page.', '.$roles_per_page;
    $roles = $db->GetAll("SELECT * FROM core_roles WHERE rid != 3 ORDER BY FIELD(rid,2,1) DESC, name $limit");
    $tpl->assign('roles', $roles);
    $tpl->assign('pageno', $pageno);
    $tpl->assign('num_of_roles', $num_of_roles);
    $tpl->assign('colspan', count($roles));
    /** end of pager **/

    /**
     * Get all the modules and perms and display only active modules
     */
    $dir = SITE_ROOT.'modules';
    $files = $db->GetCol("SELECT name FROM core_modules WHERE status=1 ORDER BY name"); // Active modules
    $modules = array();
    foreach ($files as $data) {
      $inc = "$dir/$data/$data"."_module.php";
      if (file_exists($inc)) {
        require_once $inc;
        $modules[$data]['name'] = $data;
        $modules[$data]['description'] = trim($module_description);
        $modules[$data]['perms'] = $module_perms;
        $module_description = '';
        $module_perms = '';
      }
    }

    /**
     * if available modules set perms
     */

    if (empty($modules)) {
    	$admin_perms = null;
    } else {
      /**
       * Set perms for ADMIN rid 3
       */
      foreach ($modules as $key => $value) {
        $admin_perms[$key] = $value['perms'];
      }

      $rs = $db->GetOne('SELECT perms from core_permissions WHERE rid=3');
      if ($rs != serialize($admin_perms)) {
        $d_admin['rid'] = 3;
        $d_admin['perms'] = serialize($admin_perms);
        $rs = @$db->AutoExecute('core_permissions', $d_admin, 'INSERT');
        if(!$rs) {
          $rs = $db->AutoExecute('core_permissions', $d_admin, 'UPDATE', 'rid=3');
        }
        $db->CacheFlush();
      }
    }
    /** end admin perms**/

    $tpl->assign('modules_perms', $modules);
    $rs = $db->GetAll("SELECT * FROM core_permissions");

    foreach ($rs as $value) {
      $perms = unserialize($value['perms']);
      if (!empty($perms)) {
        foreach ($perms as $key => $val) {
          $perms[$key] = array_flip($val);
          $cperms[$value['rid']] = $perms;
        }
      }
    }

    if (isset($cperms)) {
      $tpl->assign('cperms', $cperms);
    }
} // end of switch

?>